package org.gdstash.db.criteria;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;

import java.util.List;

public class StashIDSuffixCombination extends StashIDPrefixCombination {
    public StashIDSuffixCombination() {
    }

    public StashIDSuffixCombination(final StashIDSuffixCombination combo) {
        super(combo);
    }

    public StashIDSuffixCombination(final SelectionCriteria criteria) {
        super(criteria);
    }

    @Override
    public StashIDSuffixCombination clone() {
        final StashIDSuffixCombination combo = new StashIDSuffixCombination(this);
        return combo;
    }

    public static List<Integer> getStashIDs(final SelectionCriteria criteria, final CriteriaCombination.Soulbound soulbound, final CriteriaCombination.SC_HC schc, final boolean isHardcore, final String charName) {
        final StashIDSuffixCombination combo = new StashIDSuffixCombination(criteria);
        return StashIDItemCombination.getStashIDs(combo, soulbound, schc, isHardcore, charName);
    }

    @Override
    public String determineLevel1Statement(final CriteriaCombination.Soulbound soulbound, final CriteriaCombination.SC_HC schc, final boolean isHardcore, final String charName) {
        String command = "SELECT S.STASH_ID FROM STASH_ITEM_V10 S, GD_AFFIX A";
        final boolean statFilled = !this.getCriteria().statInfos.isEmpty();
        if (this.getCriteria().isSkillBonusUsed()) {
            command = command + ", " + "GD_AFFIX_SKILLS" + " ASB, " + "GD_AFFIX_SKILL_LEVEL" + " ASBL";
        }
        if (this.getCriteria().isSkillModifierUsed()) {
            command = command + ", " + "GD_AFFIX_SKILLMODIFIER" + " ASM";
        }
        if (statFilled) {
            command = command + ", " + "GD_AFFIX_STAT" + " AST";
        }
        command += " WHERE ";
        if (this.getCriteria().isSkillBonusUsed()) {
            command += "A.AFFIX_ID = ASB.AFFIX_ID AND A.AFFIX_ID = ASBL.AFFIX_ID AND ASB.TYPE = ASBL.TYPE AND ASB.INDEX = ASBL.INDEX AND ";
        }
        if (this.getCriteria().isSkillModifierUsed()) {
            command += "A.AFFIX_ID = ASM.AFFIX_ID AND ";
        }
        if (statFilled) {
            command += "A.AFFIX_ID = AST.AFFIX_ID AND ";
        }
        command += "S.SUFFIX_ID = A.AFFIX_ID AND A.TYPE = 2 AND S.ITEM_CLASS = ?";
        if (schc == CriteriaCombination.SC_HC.SOFTCORE) {
            command += " AND S.HARDCORE = false";
        }
        if (schc == CriteriaCombination.SC_HC.HARDCORE) {
            command += " AND S.HARDCORE = true";
        }
        if (schc == CriteriaCombination.SC_HC.SETTING && !GDStashFrame.iniConfig.sectRestrict.transferSCHC) {
            if (isHardcore) {
                command += " AND S.HARDCORE = true";
            } else {
                command += " AND S.HARDCORE = false";
            }
        }
        if (soulbound == CriteriaCombination.Soulbound.EXCLUDED) {
            command += " AND CHARNAME = ''";
        }
        if (soulbound == CriteriaCombination.Soulbound.INCLUDED) {
            if (this.getCriteria().soulboundMode == SelectionCriteria.SoulboundMode.NONBOUND) {
                command += " AND CHARNAME = ''";
            }
            if (this.getCriteria().soulboundMode == SelectionCriteria.SoulboundMode.SOULBOUND) {
                command += " AND CHARNAME <> ''";
            }
        }
        if (soulbound == CriteriaCombination.Soulbound.SETTING) {
            if (this.getCriteria().soulboundMode == SelectionCriteria.SoulboundMode.ALL && !GDStashFrame.iniConfig.sectRestrict.transferSoulbound) {
                if (charName == null) {
                    command += " AND CHARNAME = ''";
                } else if (charName.isEmpty()) {
                    command += " AND CHARNAME = ''";
                } else {
                    command = command + " AND ( CHARNAME = '" + charName + "' OR CHARNAME = '' )";
                }
            }
            if (this.getCriteria().soulboundMode == SelectionCriteria.SoulboundMode.NONBOUND) {
                command += " AND CHARNAME = ''";
            }
            if (this.getCriteria().soulboundMode == SelectionCriteria.SoulboundMode.SOULBOUND) {
                if (GDStashFrame.iniConfig.sectRestrict.transferSoulbound) {
                    command += " AND CHARNAME <> ''";
                } else if (charName == null) {
                    command += " AND CHARNAME = '___xxx___'";
                } else if (charName.isEmpty()) {
                    command += " AND CHARNAME = '___xxx___'";
                } else {
                    command = command + " AND CHARNAME = '" + charName + "'";
                }
            }
        }
        if (this.usesItemRarity()) {
            command += " AND S.RARITY = ?";
        }
        if (this.usesArmorClass()) {
            command += " AND S.ARMOR_CLASS = ?";
        }
        if (this.usesArtifactClass()) {
            command += " AND S.ARTIFACT_CLASS = ?";
        }
        if (this.usesItemName()) {
            command += " AND S.NAME LIKE ?";
        }
        if (this.usesSetItem()) {
            command += " AND S.SET_ID IS NOT NULL";
        }
        if (this.usesBonusSkill()) {
            command += " AND ASB.BONUS_ENTITY = ? AND ASBL.BONUS_VALUE > 0";
        }
        if (this.usesMasteryBonusSkills()) {
            command += " AND ASB.MASTERY_ID = ? AND ASBL.BONUS_VALUE > 0";
        }
        if (this.usesAllSkills()) {
            command += " AND S.PLUS_ALLSKILLS > 0";
        }
        if (this.usesSkillModifier()) {
            command += " AND ASM.SKILL_ID = ?";
        }
        if (this.usesMasteryModifySkills()) {
            command += " AND ASM.MASTERY_ID = ?";
        }
        if (this.usesAllItemSkills()) {
            command += " AND A.ITEM_SKILL_ID IS NOT NULL";
        }
        if (this.usesItemSkill()) {
            command += " AND A.ITEM_SKILL_ID = ?";
        }
        if (this.getMinLevel() != -1) {
            command += " AND S.REQ_LEVEL >= ?";
        }
        if (this.getMaxLevel() != -1) {
            command += " AND S.REQ_LEVEL <= ?";
        }
        if (this.getMaxCunning() != -1) {
            command += " AND S.REQ_DEX <= ?";
        }
        if (this.getMaxPhysique() != -1) {
            command += " AND S.REQ_STR <= ?";
        }
        if (this.getMaxSpirit() != -1) {
            command += " AND S.REQ_INT <= ?";
        }
        if (this.usesPetBonus()) {
            command += " AND A.PET_AFFIX_ID IS NOT NULL";
        }
        if (this.usesConversionFrom()) {
            if (this.usesConversionTo()) {
                command += " AND (( A.CONVERT_IN = ? AND A.CONVERT_OUT = ? ) OR ( A.CONVERT_IN_2 = ? AND A.CONVERT_OUT_2 = ? ))";
            } else {
                command += " AND (( A.CONVERT_IN = ? ) OR ( A.CONVERT_IN_2 = ? ))";
            }
        } else if (this.usesConversionTo()) {
            command += " AND (( A.CONVERT_OUT = ? ) OR ( A.CONVERT_OUT_2 = ? ))";
        }
        if (this.isNoEnemyOnly()) {
            command += " AND S.ENEMY_ONLY = ?";
        }
        command += this.determineSlotConditions("S.");
        return command;
    }
}
